package com.java.module.chat.controller.admin;

import java.util.List;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.java.common.lang.domain.ReturnVO;
import com.java.module.chat.domain.dto.ChatMessageDTO;
import com.java.module.chat.domain.vo.ChatMessageVO;
import com.java.module.chat.domain.ChatMessage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.java.common.lang.annotation.Log;
import com.java.common.lang.domain.AjaxResult;
import com.java.common.lang.domain.page.TableDataInfo;
import com.java.common.lang.enums.BusinessTypeEnum;
import com.java.common.utils.WebKit;
import com.java.common.utils.poi.ExcelUtil;
import com.java.module.chat.service.IChatMessageService;

/**
 * 聊天：消息Controller
 * 
 * @author Administrator
 * @date 2021-05-13
 */
@RestController("AdminChatMessageController")
@RequestMapping("/chat/message")
public class ChatMessageController {

    @Autowired
    private IChatMessageService chatMessageService;

    /**
     * 查询聊天：消息列表
     */
    @PreAuthorize("@ss.hasPermi('chat:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChatMessage chatMessage) {
        WebKit.startPage();
        List<ChatMessageVO> list = chatMessageService.selectChatMessageHistory(chatMessage);
        return WebKit.getDataTable(list);
    }

    /**
     * 导出聊天：消息列表
     */
    @PreAuthorize("@ss.hasPermi('chat:message:export')")
    @Log(title = "聊天：消息", businessType = BusinessTypeEnum.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ChatMessage chatMessage) {
        List<ChatMessage> list = chatMessageService.selectChatMessageList(chatMessage);
        ExcelUtil<ChatMessage> util = new ExcelUtil<ChatMessage>(ChatMessage.class);
        return util.exportExcel(list, "message");
    }

    /**
     * 获取聊天：消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('chat:message:query')")
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable("messageId") Long messageId) {
        return AjaxResult.success(chatMessageService.selectChatMessageById(messageId));
    }

    /**
     * 新增聊天：消息
     */
    @PreAuthorize("@ss.hasPermi('chat:message:add')")
    @Log(title = "聊天：消息", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChatMessage chatMessage) {
        return WebKit.toAjax(chatMessageService.insertChatMessage(chatMessage));
    }

    /**
     * 修改聊天：消息
     */
    @PreAuthorize("@ss.hasPermi('chat:message:edit')")
    @Log(title = "聊天：消息", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatMessage chatMessage) {
        return WebKit.toAjax(chatMessageService.updateChatMessage(chatMessage));
    }

    /**
     * 删除聊天：消息
     */
    @PreAuthorize("@ss.hasPermi('chat:message:remove')")
    @Log(title = "聊天：消息", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable Long[] messageIds) {
        return WebKit.toAjax(chatMessageService.deleteChatMessageByIds(messageIds));
    }


    /**
     * 发送聊天消息
     */
    @PostMapping("/sendMessage")
    public ReturnVO<ChatMessageVO> sendMessage(@Validated @RequestBody ChatMessageDTO body) {
        body.setMsgFromUid(0L);
        return chatMessageService.sendMessage(body);
    }

    @ApiOperationSupport(order = 30)
    @ApiOperation("设置已读")
    @PostMapping("/readAllMessage")
    public int readAllMessage(@RequestBody ChatMessageDTO body) {
        body.setMsgToUid(0L);
        return chatMessageService.readAllMessage(body);
    }

}